<?php

/**
 * sfWidgetFormoctrineSelectStatus.
 *
 * @package    scart
 * @subpackage actions
 * @author     Pedro Nilton Olivera Chamochumbi <poliverach@gmail.com>
 * @author     Jonathan Olger Nieto Lajo <jonathan_nieto@hotmail.com>
 */

class sfWidgetFormDoctrineStatusSelect extends sfWidgetFormDoctrineSelect
{
  protected function configure($options = array(), $attributes = array())
  {
    parent::configure($options, $attributes);
    $this->setOption('method', 'getDoctrineStatusSelect');
  }
	
  protected function getOptionsForSelect($value, $choices)
  {
    $mainAttributes = $this->attributes;
    $this->attributes = array();

    $options = array();
    foreach ($choices as $key => $option)
    {
      if (is_array($option))
      {
        $options[] = $this->renderContentTag('optgroup', implode("\n", $this->getOptionsForSelect($value, $option)), array('label' => self::escapeOnce($key)));
      }
      else
      {
        $attributes = array('value' => self::escapeOnce($key));
        if ((is_array($value) && in_array(strval($key), $value)) || strval($key) == strval($value))
        {
          $attributes['selected'] = 'selected';
        }
        
        $status = substr($option, 0, 1);
        $option = substr($option, 1);
        if (kcDoctrineRecord::INACTIVE === $status)
        {
          $attributes['class'] = 'inactive';
        }

        $options[] = $this->renderContentTag('option', self::escapeOnce($option), $attributes);
      }
    }

    $this->attributes = $mainAttributes;

    return $options;
  }
}